import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '../views/auth/LoginView.vue'
import RegisterView from '../views/auth/RegisterView.vue'
import ForumView from '../views/forum/ForumView.vue'
import CategoryView from '../views/forum/CategoryView.vue'
import PostView from '../views/forum/PostView.vue'
import BlogDetailView from '../views/forum/BlogDetailView.vue'
import DataView from '../views/data/DataView.vue'
import CharacterDataView from '../views/data/CharacterDataView.vue'
import DungeonDataView from '../views/data/DungeonDataView.vue'
import CommunityView from '../views/community/CommunityView.vue'
import MessagesView from '../views/community/MessagesView.vue'
import ProfileView from '../views/user/ProfileView.vue'
import SettingsView from '../views/user/SettingsView.vue'

const router = createRouter({
  history: createWebHistory('/'),
  routes: [
    {
      path: '/',
      redirect: '/forum'
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView
    },
    {
      path: '/register',
      name: 'register',
      component: RegisterView
    },
    {
      path: '/forum',
      name: 'forum',
      component: ForumView
    },
    {
      path: '/forum/category/:id',
      name: 'category',
      component: CategoryView
    },
    {
      path: '/forum/post/:id',
      name: 'post',
      component: PostView
    },
    {
      path: '/forum/tag/:id',
      name: 'tag',
      component: CategoryView
    },
    {
      path: '/forum/blog/:id',
      name: 'blog-detail',
      component: BlogDetailView
    },
    {
      path: '/data',
      name: 'data',
      component: DataView
    },
    {
      path: '/data/character',
      name: 'character-data',
      component: CharacterDataView
    },
    {
      path: '/data/dungeon',
      name: 'dungeon-data',
      component: DungeonDataView
    },
    {
      path: '/community',
      name: 'community',
      component: CommunityView
    },
    {
      path: '/messages',
      name: 'messages',
      component: MessagesView,
      meta: { requiresAuth: true }
    },
    {
      path: '/profile',
      name: 'profile',
      component: ProfileView,
      meta: { requiresAuth: true }
    },
    {
      path: '/user/:id',
      name: 'user-profile',
      component: ProfileView
    },
    {
      path: '/settings',
      name: 'settings',
      component: SettingsView,
      meta: { requiresAuth: true }
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token')
  
  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login')
  } else {
    next()
  }
})

export default router
